package com.only4play.practice.ordercenter.core.events;

import com.only4play.practice.common.enums.OrderOperateType;
import com.only4play.practice.ordercenter.api.model.domainmodel.OrderItemModel;
import com.only4play.practice.ordercenter.model.es.OrderDocument;
import com.only4play.practice.ordercenter.core.entity.OrderBase;
import com.only4play.practice.ordercenter.core.entity.OrderItem;
import com.only4play.practice.ordercenter.core.entity.OrderLifecycle;
import com.only4play.practice.ordercenter.core.mapper.OrderItemMapper;
import com.only4play.practice.ordercenter.core.mapper.OrderLifecycleMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
@RequiredArgsConstructor
@Slf4j
public class OrderEventProcessor {


    @Resource
    private OrderLifecycleMapper orderLifecycleMapper;
    @Resource
    private OrderItemMapper orderItemMapper;

    @EventListener
    public void handleOrderCreateForItem(OrderEvents.OrderCreateEvent orderCreateEvent){
        List<OrderItemModel> itemInfoList = orderCreateEvent.getCreateModel().getItemInfoList();
        itemInfoList.forEach(itemInfo -> {
            log.info("handleOrderCreateForItem：{}",itemInfo);
            OrderItem orderItem = new OrderItem();
            orderItem.setOrderId(orderCreateEvent.getOrderBase().getId());
            orderItem.setFlowNo(orderCreateEvent.getOrderBase().getFlowNo());
            orderItem.setRealAmount(itemInfo.getRealAmount());
            orderItem.setItemCount(itemInfo.getItemCount());
            orderItem.setSkuId(Long.valueOf(itemInfo.getSkuId()));
            orderItem.setGoodsName(itemInfo.getGoodsName());
            orderItem.setFeeRemark(itemInfo.getFeeRemark());
            orderItemMapper.insert(orderItem);
        });
    }

    @EventListener
    public void handleOrderCreateForLifecycle(OrderEvents.OrderCreateEvent createEvent){
        OrderLifecycle orderLifecycle = new OrderLifecycle();
        orderLifecycle.setFlowNo(createEvent.getOrderBase().getFlowNo());
        orderLifecycle.setOperateType(OrderOperateType.ORDER_CREATE);
        orderLifecycle.setOperateUser(createEvent.getCreateModel().getOperateUser());
        orderLifecycle.setRemark("订单创建成功");
        orderLifecycle.setValidStatus(createEvent.getOrderBase().getValidStatus());
        int insert = orderLifecycleMapper.insert(orderLifecycle);
        log.info("handleOrderCreateForLifecycle：{}",insert);
    }

    @EventListener
    public void handleOrderCreateForEs(OrderEvents.OrderCreateEvent createEvent){
        OrderBase orderBase = createEvent.getOrderBase();
        OrderDocument document = new OrderDocument();
        document.setFlowNo(String.valueOf(orderBase.getFlowNo()));
        document.setId(String.valueOf(orderBase.getFlowNo()));
        document.setOrderStatusTxt(orderBase.getOrderState().getName());
        document.setOrderTypeTxt(orderBase.getOrderType().getName());
        document.setOrderItems(createEvent.getCreateModel().getItemInfoList());
        log.info("handleOrderCreateForEs：{}",document);
//        orderDocumentRepository.save(document);
    }

}
